home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v2.1 / Amiga Developer CD v2.1.iso / Contributions / Haage_&_Partner / Storm-Projects / NDKExamples1 / intuition / makevisible.c < prev    next >
C/C++ Source or Header  |  1999-04-16  |  6KB  |  245 lines

  1. /*
  2.  * makevisible.c - shows off SPOS_MAKEVISIBLE feature
  3.  *
  4.  * (c) Copyright 1992-1996 Amiga International, Inc. All rights reserved.
  5.  *
  6.  * This software is provided as-is and is subject to change; no warranties
  7.  * are made.  All use is at your own risk.  No liability or responsibility
  8.  * is assumed.
  9.  *
  10.  * Opens an oversized autoscrolling screen.  Use the mouse to draw
  11.  * a rectangle on the screen.  Then, use the mouse to scroll the screen
  12.  * anywhere you like.  Press any key to move the drawn rectangle
  13.  * into view using ScreenPosition( sc, SPOS_MAKEVISIBLE, ... ).
  14.  *
  15.  * The SPOS_MAKEVISIBLE feature can be useful to ensure certain areas
  16.  * are visible, for example the cursor of a word-processor.
  17.  *
  18.  * Press "Q" or <Esc> to exit.
  19.  *
  20.  */
  21.  
  22. /*----------------------------------------------------------------------*/
  23.  
  24. #include <exec/types.h>
  25. #include <graphics/displayinfo.h>
  26. #include <graphics/gfxmacros.h>
  27. #include <graphics/gfxbase.h>
  28. #include <intuition/intuitionbase.h>
  29.  
  30. #include <clib/exec_protos.h>
  31. #include <clib/graphics_protos.h>
  32. #include <clib/intuition_protos.h>
  33.  
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36.  
  37. /*----------------------------------------------------------------------*/
  38.  
  39. void error_exit(STRPTR errorstring);
  40.  
  41. /*----------------------------------------------------------------------*/
  42.  
  43. struct GfxBase *GfxBase = NULL;
  44. struct IntuitionBase *IntuitionBase = NULL;
  45. struct Screen *myscreen = NULL;
  46. struct Window *mywindow = NULL;
  47.  
  48. /*----------------------------------------------------------------------*/
  49.  
  50. UWORD pens[] =
  51. {
  52.     0, /* DETAILPEN */
  53.     1, /* BLOCKPEN    */
  54.     1, /* TEXTPEN    */
  55.     2, /* SHINEPEN    */
  56.     1, /* SHADOWPEN    */
  57.     3, /* FILLPEN    */
  58.     2, /* FILLTEXTPEN    */
  59.     0, /* BACKGROUNDPEN    */
  60.     2, /* HIGHLIGHTTEXTPEN    */
  61.  
  62.     1, /* BARDETAILPEN    */
  63.     2, /* BARBLOCKPEN    */
  64.     1, /* BARTRIMPEN    */
  65.  
  66.     (UWORD)~0,
  67. };
  68.  
  69. /*----------------------------------------------------------------------*/
  70.  
  71. /*  MYDISPLAYID represents the desired mode, and should be taken from
  72.     graphics/displayinfo.h */
  73. #define MYDISPLAYID    (LORES_KEY)
  74. #define MYWIDTH    900
  75. #define MYHEIGHT 600
  76.  
  77. /*----------------------------------------------------------------------*/
  78.  
  79. main()
  80. {
  81.     BOOL terminated = FALSE;
  82.     BOOL dragging = FALSE;
  83.     struct IntuiMessage *imsg;
  84.     struct Rectangle drawn_rect, drag_rect;
  85.  
  86.     if (!( GfxBase = (struct GfxBase *)
  87.     OpenLibrary("graphics.library", 39L) ))
  88.     {
  89.     error_exit("Couldn't open Gfx V39\n");
  90.     }
  91.  
  92.     if (!( IntuitionBase = (struct IntuitionBase *)
  93.     OpenLibrary("intuition.library", 39L) ))
  94.     {
  95.     error_exit("Couldn't open Intuition V39\n");
  96.     }
  97.  
  98.     if (!(myscreen = OpenScreenTags(NULL,
  99.     SA_DisplayID, MYDISPLAYID,
  100.     SA_Overscan, OSCAN_TEXT,
  101.     /*  Other tags can go here: */
  102.     SA_Width, MYWIDTH,
  103.     SA_Height, MYHEIGHT,
  104.     SA_Depth, 2,
  105.     SA_AutoScroll, 1,
  106.     SA_Pens, pens,
  107.     SA_Title, "Draw a rectangle with the mouse.  Scroll it off-screen.  Hit a key to scroll it into view.  <Esc> to quit.",
  108.     TAG_DONE )))
  109.     {
  110.     error_exit("Couldn't open screen\n");
  111.     }
  112.     if (!( mywindow = OpenWindowTags(NULL,
  113.     WA_Borderless, TRUE,
  114.     WA_Backdrop, TRUE,
  115.     WA_IDCMP, IDCMP_MOUSEBUTTONS|IDCMP_VANILLAKEY,
  116.     WA_NoCareRefresh, TRUE,
  117.     WA_Activate, TRUE,
  118.     WA_SmartRefresh, TRUE,
  119.     WA_CustomScreen, myscreen,
  120.     TAG_DONE ) ))
  121.     {
  122.     error_exit("Couldn't open window\n");
  123.     }
  124.  
  125.     drawn_rect.MinX = 20;
  126.     drawn_rect.MinY = 20;
  127.     drawn_rect.MaxX = 150;
  128.     drawn_rect.MaxY = 100;
  129.     SetABPenDrMd( mywindow->RPort, 3, 0, COMPLEMENT );
  130.     SetOPen( mywindow->RPort, 1 );
  131.     RectFill( mywindow->RPort, drawn_rect.MinX, drawn_rect.MinY,
  132.     drawn_rect.MaxX, drawn_rect.MaxY );
  133.  
  134.     while ( !terminated )
  135.     {
  136.     Wait( 1 << mywindow->UserPort->mp_SigBit );
  137.     while ( imsg = (struct IntuiMessage *)GetMsg( mywindow->UserPort ) )
  138.     {
  139.         switch ( imsg->Class )
  140.         {
  141.         case IDCMP_VANILLAKEY:
  142.         if ( ( imsg->Code == 0x1B ) || ( imsg->Code == 'q' ) || ( imsg->Code == 'Q' ) )
  143.         {
  144.             terminated = TRUE;
  145.         }
  146.         else
  147.         {
  148.             ScreenPosition( myscreen, SPOS_MAKEVISIBLE,
  149.             drawn_rect.MinX, drawn_rect.MinY,
  150.             drawn_rect.MaxX, drawn_rect.MaxY );
  151.         }
  152.         break;
  153.  
  154.         case IDCMP_MOUSEBUTTONS:
  155.         {
  156.             WORD mousex = imsg->MouseX;
  157.             WORD mousey = imsg->MouseY;
  158.  
  159.             if ( mousex < 0 ) mousex = 0;
  160.             if ( mousex >= myscreen->Width ) mousex = myscreen->Width - 1;
  161.             if ( mousey < 0 ) mousey = 0;
  162.             if ( mousey >= myscreen->Height ) mousey = myscreen->Height - 1;
  163.  
  164.             if ( imsg->Code == SELECTDOWN )
  165.             {
  166.             dragging = TRUE;
  167.             drag_rect.MinX = mousex;
  168.             drag_rect.MinY = mousey;
  169.             }
  170.             else if ( ( imsg->Code == SELECTUP ) && ( dragging ) )
  171.             {
  172.             dragging = FALSE;
  173.             if ( mousex > drag_rect.MinX )
  174.             {
  175.                 drag_rect.MaxX = mousex;
  176.             }
  177.             else
  178.             {
  179.                 drag_rect.MaxX = drag_rect.MinX;
  180.                 drag_rect.MinX = mousex;
  181.             }
  182.             if ( mousey > drag_rect.MinY )
  183.             {
  184.                 drag_rect.MaxY = mousey;
  185.             }
  186.             else
  187.             {
  188.                 drag_rect.MaxY = drag_rect.MinY;
  189.                 drag_rect.MinY = mousey;
  190.             }
  191.             SetOPen( mywindow->RPort, 0 );
  192.             RectFill( mywindow->RPort, drawn_rect.MinX, drawn_rect.MinY,
  193.                 drawn_rect.MaxX, drawn_rect.MaxY );
  194.             drawn_rect = drag_rect;
  195.             SetOPen( mywindow->RPort, 1 );
  196.             RectFill( mywindow->RPort, drawn_rect.MinX, drawn_rect.MinY,
  197.                 drawn_rect.MaxX, drawn_rect.MaxY );
  198.             }
  199.         }
  200.         break;
  201.         }
  202.         ReplyMsg((struct Message *) imsg );
  203.     }
  204.     }
  205.     error_exit(NULL);
  206. }
  207.  
  208.  
  209. /*----------------------------------------------------------------------*/
  210.  
  211. void error_exit(STRPTR errorstring)
  212.  
  213.     {
  214.     if (mywindow)
  215.     {
  216.     CloseWindow(mywindow);
  217.     }
  218.  
  219.     if (myscreen)
  220.     {
  221.     CloseScreen(myscreen);
  222.     }
  223.  
  224.     if (IntuitionBase)
  225.     {
  226.     CloseLibrary((struct Library *)IntuitionBase);
  227.     }
  228.  
  229.     if (GfxBase)
  230.     {
  231.     CloseLibrary((struct Library *)GfxBase);
  232.     }
  233.  
  234.     if (errorstring)
  235.     {
  236.     printf(errorstring);
  237.     exit(20);
  238.     }
  239.  
  240.     exit(0);
  241.     }
  242.  
  243.  
  244. /*----------------------------------------------------------------------*/
  245.